package com.app.micaihu.view.set;

import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseListActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.infor.BlacklistBean;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.components.ParamMap;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;
import com.app.micaihu.view.user.userinfo.adapter.BlacklistAdapter;
import com.blankj.utilcode.util.ActivityUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * 黑名单页面
 * Created by Guo on 2016/12/26.
 */

public class BlacklistActivity extends BaseListActivity<BlacklistBean> {


    @Override
    protected void changeView() {
        super.changeView();
        setTitle(getResources().getString(R.string.set_blacklist));
    }

    @Override
    protected void changeData(final boolean isRefresh) {
        ParamMap paramMap = new ParamMap();
        if (UserInforManager.getInstance().isLogin()) {
            paramMap.put("uid", UserInforManager.getInstance().getUserInfor().getUid());

        } else {
            paramMap.put("uid", "0");
        }
        paramMap.put("page", dataPage + "");
        paramMap.put("getAll", "0");//是否拉取全部 1是 0否
        HttpUtils.ParmsMapMD5(paramMap);

        HttpListener<DataBean<List<BlacklistBean>>> blackListListener = new HttpListener<DataBean<List<BlacklistBean>>>() {

            @Override
            public void onStart() {
                super.onStart();
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
            }

            @Override
            public void onSuccess(DataBean<List<BlacklistBean>> response) {
                if (response.noError()) {
                    List<BlacklistBean> blacklists = response.getData();
                    if (blacklists != null) {
                        if (!blacklists.isEmpty()) {
                            if (dataList == null) {
                                dataList = new ArrayList<BlacklistBean>();
                            }
                            if (isRefresh) {
                                //清空数据
                                dataList.clear();
                            }
                            dataList.addAll(blacklists);

                            if (mAdapter == null) {
                                mAdapter = new BlacklistAdapter(dataList, BlacklistActivity.this);
                                mListView.setAdapter(mAdapter);
                            } else {
                                mAdapter.notifyDataSetChanged();
                            }
                            setLoadView(LOADSUCES, null);
                        } else {
                            setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getString(R.string.set_blacklist_empty), null, null);
                        }

                    }
                } else {
                    setLoadView(DATAERROR, response.getNnderstoodMsg());
                }
            }
        };


        addPostCacheRequest(URIContant.BLACKLIST_LIST, new TypeToken<DataBean<List<BlacklistBean>>>() {
        }.getType(), paramMap, blackListListener);


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //因为有头部
        if (mListView.getRefreshableView().getHeaderViewsCount() > 0) {
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        if (position >= dataList.size()) {
            position = dataList.size() - 1;
        } else if (position < 0) {
            position = 0;
        }
        BlacklistBean blacklistBean = dataList.get(position);
        if (blacklistBean == null) return;
        String toUid = blacklistBean.getUid();
        MyHomepageDynamicActivity.goToUserHomePage(ActivityUtils.getTopActivity(), toUid);
    }
}
